package kbpost

import (
	"context"
	"mall/common/globalkey"
	"mall/common/jwt"
	"mall/common/mqueuex"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/blog/rpc/blogclient"
	"mall/service/forum/api/internal/logic/util"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/user/rpc/userclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddEntryFavoriteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddEntryFavoriteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddEntryFavoriteLogic {
	return &AddEntryFavoriteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddEntryFavoriteLogic) AddEntryFavorite(
	req *types.AddEntryFavoriteReq,
	headers *[]*types.Header,
) (resp *types.BaseMsgResp, err error) {
	if req.EntryId == nil || *req.EntryId == 0 {
		return nil, resultx.NewErrCode(resultx.ENTRY_PERFUME_ID_REQUIRED)
	}

	if req.Type == nil || *req.Type == 0 {
		return nil, resultx.NewErrCode(resultx.FORUM_REACTION_TYPE_INVALID)
	}

	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}

	_, err = l.svcCtx.BlogRpc.LogReaction(l.ctx, &blogclient.LogReactionReq{
		PostId:   *req.EntryId,
		PostType: uint32(globalkey.PostTypeKnowledgeBase),
		UserId:   userId,
		Type:     util.GetReactionType(*req.Type),
		Status:   uint32(globalkey.StatusEnabled),
	})
	if err != nil {
		logx.Errorw("AddEntryFavorite BlogRpc.LogReaction err", logx.Field("detail", err.Error()))
		return nil, err
	}

	var metaKey *string
	var userMeta []*userclient.UserMetaInfo
	switch *req.Type {
	case globalkey.ForumReactionTypeWant: // 想要
		metaKey = pointy.GetPointer(mqueuex.UserStatisticWantEntryCount)
	case globalkey.ForumReactionTypeOwn: // 拥有
		metaKey = pointy.GetPointer(mqueuex.UserStatisticOwnEntryCount)
		userMeta = append(userMeta, &userclient.UserMetaInfo{
			Key:   pointy.GetPointer(mqueuex.UserStatisticOwnEntryNew), // "_own_entry_status"
			Value: pointy.GetPointer("-1"),
		})
	}
	_, err = l.svcCtx.UserRpc.SetUserMeta(l.ctx, &userclient.SetUserMetaReq{
		UserId:   pointy.GetPointer(userId),
		MetaKey:  metaKey,
		Incr:     pointy.GetPointer(int32(1)),
		UserMeta: userMeta,
	})
	if err != nil {
		logx.Errorw("AddEntryFavorite UserRpc.SetUserMeta err", logx.Field("detail", err.Error()))
	}

	//err = l.createFeedBack(globalkey.GorseKnowledgeBasePrefix+strconv.FormatUint(*req.EntryId, 10), strconv.FormatUint(userId, 10))
	//if err != nil {
	//	logx.Errorf("AddEntryFavorite createFeedBack Error: %v", err)
	//}

	return &types.BaseMsgResp{
		Msg: "操作成功",
	}, nil
}

func (l *AddEntryFavoriteLogic) createFeedBack(pid, uid string) error {
	return l.svcCtx.GorseClient.CreateFeedback(
		l.svcCtx.Config.Query.GorseUrl,
		pid, uid,
		globalkey.RecommendedFeedBackTypeCollect,
	)
}

//docker run --restart=always --name kafka -p 9092:9092 \
//-e KAFKA_BROKER_ID=0 \
//-e KAFKA_ZOOKEEPER_CONNECT=193.112.95.184:2181/kafka \
//-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://193.112.95.184:9092 \
//-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 \
//-v /etc/localtime:/etc/localtime -d wurstmeister/kafka
